package cn.tedu.circle.admin.account.service.impl;

import cn.tedu.circle.admin.account.dao.persist.repository.IUserRepository;
import cn.tedu.circle.admin.account.pojo.entity.User;
import cn.tedu.circle.admin.account.pojo.param.UserUpdateInfoParam;
import cn.tedu.circle.admin.account.pojo.vo.UserListItemVO;
import cn.tedu.circle.admin.account.service.IUserService;
import cn.tedu.circle.common.pojo.vo.PageData;
import cn.tedu.circle.common.web.JsonResult;
import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserRepository iUserRepository;

    @Override
    public PageData<UserListItemVO> list(Integer pageNum, Integer pageSize){
        return iUserRepository.list(pageNum,pageSize);
    }
    @Override
    public int deleteById(Long id) {
        if (id == null) {
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, "删除失败,id不存在!");
        }
        return iUserRepository.deleteById(id);
    }

    @Override
    public int updateById(UserUpdateInfoParam userUpdateInfoParam) {
        User user = new User();
        BeanUtils.copyProperties(userUpdateInfoParam, user);
        return iUserRepository.updateInfo(userUpdateInfoParam);
    }

    @Override
    public void updateRole(Long id, Integer roleId) {
        User user = new User();
        user.setId(id);
        user.setRoleId(roleId);
        iUserRepository.updateById(user);
    }

    @Override
    public void updateEnable(Long id, Integer enable) {
        if (enable==1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"用户已启用");
        }
        if (enable!=1&&enable!=0){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"禁用状态不匹配");
        }
        User user = new User();
        user.setId(id);
        user.setEnable(enable);
        iUserRepository.updateById(user);
    }




}
